package com.sxt.sys.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sxt.sys.SYSConstast.SYSConstast;
import com.sxt.sys.domain.Permission;
import com.sxt.sys.domain.User;
import com.sxt.sys.service.PermissionService;
import com.sxt.sys.utils.TreeNode;
import com.sxt.sys.utils.TreeNodeBuilder;
import com.sxt.sys.utils.WebUtils;
import com.sxt.sys.vo.PermissionVo;

@RestController
@RequestMapping("menu")
public class MenuController {
	@Autowired
	private PermissionService permissionService;
	
	@RequestMapping("loadmenu")
	public List<TreeNode> loadmenu(PermissionVo permissionVo){
		List<TreeNode> nodes = new ArrayList<>();
		User user = WebUtils.getCurrentUser();
		List<Permission> permissions=null;
		permissionVo.setType(SYSConstast.MENU_TYPE_MENU);
		permissionVo.setAvailable(SYSConstast.MENU_AVAILABLE_TRUE);
		if(user.getType()==SYSConstast.USER_TYPE_SUPER) {
			permissions = permissionService.queryAllPermisson(permissionVo);
		}else {
			
			permissions = permissionService.queryApartPermissonByUserId(user.getId());
		}
		
		for (Permission permission : permissions) {
			Integer id = permission.getId();
			Integer pid = permission.getPid();
			String title = permission.getName();
			String icon = permission.getIcon();
			String href = permission.getHref();
			Boolean spread = permission.getOpen()==SYSConstast.MENU_OPEN_TRUE?true:false;
			nodes.add(new TreeNode(id, pid, title, icon, href, spread));
		}
		
		return TreeNodeBuilder.build(nodes, 1);
	}

	
	
}
